﻿namespace MSRI.SmsSessions
{
    using System;
    using System.Collections.Generic;
    using System.Reflection;

    public class SmsSessions
    {
        private List<SmsSession> _SmsSessionList = new List<SmsSession>();

        public void Add(SmsSession SmsSessionItem)
        {
            if (this[SmsSessionItem.SessionName] != null)
            {
                throw new Exception(string.Format("Error:Session with SessioName:{0} already exist", SmsSessionItem.SessionName));
            }
            this._SmsSessionList.Add(SmsSessionItem);
        }

        public void Remove(SmsSession SmsSessionItem)
        {
            this._SmsSessionList.Remove(SmsSessionItem);
        }

        public void Remove(string SessionName)
        {
            for (int i = 0; i < this._SmsSessionList.Count; i++)
            {
                if (this._SmsSessionList[i].SessionName == SessionName)
                {
                    this._SmsSessionList.RemoveAt(i);
                    return;
                }
            }
        }

        public int Count
        {
            get
            {
                int num = 0;
                while (num < this._SmsSessionList.Count)
                {
                    if (this._SmsSessionList[num].IsDeleted)
                    {
                        this._SmsSessionList.RemoveAt(num--);
                    }
                    num++;
                }
                return num;
            }
        }

        public SmsSession this[string SessionName]
        {
            get
            {
                for (int i = 0; i < this._SmsSessionList.Count; i++)
                {
                    if (this._SmsSessionList[i].SessionName == SessionName)
                    {
                        if (this._SmsSessionList[i].IsDeleted)
                        {
                            this._SmsSessionList.RemoveAt(i);
                            return null;
                        }
                        return this._SmsSessionList[i];
                    }
                }
                return null;
            }
        }

        public List<SmsSession> this[SmsSessionContact Contact]
        {
            get
            {
                List<SmsSession> list = new List<SmsSession>();
                for (int i = 0; i < this._SmsSessionList.Count; i++)
                {
                    if (this._SmsSessionList[i].Contains(Contact))
                    {
                        if (this._SmsSessionList[i].IsDeleted)
                        {
                            this._SmsSessionList.RemoveAt(i--);
                        }
                        else
                        {
                            list.Add(this._SmsSessionList[i]);
                        }
                    }
                }
                return list;
            }
        }

        public SmsSession this[int Index]
        {
            get
            {
                if (this._SmsSessionList[Index].IsDeleted)
                {
                    this._SmsSessionList.RemoveAt(Index);
                    return null;
                }
                return this._SmsSessionList[Index];
            }
        }
    }
}

